<?php
class UpFile_ApiClient
	{
		private static $instance=null;
		private $data=null;
		private $url='';
		private $referer='http://copr.hucai.com/index.php?a=apis.Ts_Task_list';
		private $auth='';
		private $authcode='';
		private $header='';
		public function __construct()
			{
				
			}
		private function setheader()
			{
				$header=1;
				if($header)
					{
						$this->header=array(
							'CLIENT_TOKEN'=>$this->auth,
							'CLIENT_CODE'=>$this->authcode
						);
					}
					else
						{
							return false;
						}
				return true;
				
			}
		private function setauth($auth)
			{
				if($auth)
					{
						$this->auth=$auth;
						$this->authcode=md5(substr(md5($auth),5,15).md5($auth));
					}
					else
						{
							return false;
						}
				return true;
			}
		//设置CURL参数，并获取数据	
		private function Api_Curl()
			{
				$time_out=30000;
				$timeout=10000;
				$url='';
				$referer='';
				$curl='';
				if(!$this->url && !$this->data)
					{
						return false;
					}
					try
						{
							if(!$curl=curl_init())
								{
									throw new Exception('初始化失败！');
								}
								else
									{
										
									}
						}
						catch(Exception $e)
							{
								$e->getMessage();
							}
				
				curl_setopt($curl,CURLOPT_URL,$this->url);//要转送数据到的网址（调用方）
				curl_setopt($curl,CURLOPT_REFERER,$this->referer);//发送数据的网址（被调用方）
				//curl_setopt($curl,CURLOPT_HTTPHEADER,$this->header);
				//curl_setopt($curl,CURLOPT_HEADER,'content-type:aplication/json;charset=utf-8');
				curl_setopt($curl,CURLOPT_POST,true);//推送数据的方式
				curl_setopt($curl,CURLOPT_POSTFIELDS,$this->data);//推送的数据
				curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
				curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,$time_out);//连接服务器没有反应的等待时间
				curl_setopt($curl,CURLOPT_TIMEOUT,$timeout);//接收数据时等待的时间
				
				$result_data=curl_exec($curl);//执行推送数据
				$error=curl_errno($curl);
				
				
				curl_close($curl);
				
				/* $context=stream_context_create($this->data);
				//echo $context;
				//var_dump($this->data);
				$result_data=file_get_contents($this->url,false,$context); */
				return $result_data;
			}
		//对外接口
		public function DoRequest($url,$auth,$data=null)
			{
				$sign_str=$this->set_sign($data);
				$sign=$this->get_sign($sign_str,$auth);
				$data['sign']=$sign;
				//print_r($data);
				//exit;
				if($this->setauth($auth))
					{
						if($this->setheader())
							{
								$this->url=$url;
								//$this->data=$data;
								if(!empty($data))
									{
										if(is_array($data))
											{
												//$datas= http_build_query($data);
												//echo $datas;
												foreach($data as $key => $value)
													{
														$datas.=$key.'='.urlencode($value).'&';//urlencode()
													}
													$datas=rtrim($datas,'&');
													
													/* 
													$datas=array('http'=>array(
														'method'=>'POST',
														'header'=>'Content-type:application/x-www-form-urlencodeed',
														'content'=>$datas,
														'timeout'=>900
													));
													 */
													
													$this->data=$datas;
													
													//echo $this->data;
													//exit;
													/* if($datas)
														{
															$this->url.='?'.$datas.'&auth='.urlencode($this->auth).'&code='.urlencode($this->authcode).'&';
														}
														else
															{
																$this->url.='?'.'auth='.urlencode($this->auth).'&code='.urlencode($this->authcode).'&';
															}
												echo $this->url;
												//$this->url.=rtrim($datas,'&'); */
												unset($datas);
											}
									}
									
								$result_data=$this->Api_Curl();
								return $result_data;
								
							}
					}
			}
		//类的析构函数
		public function __destruct()
			{
				
			}
		//循环输出多维数组的所有元素
		private function display_multi_array($array)
			{
				if(empty($array))
					{
						return false;
					}
					elseif(is_array($array))
						{
							$count=count($array);
							if($count>1)
								{
									foreach($array as $key => $value )
										{
											if(!is_array($value))
												{
													
													echo '<h3><span style="color:green;margin-right:15px;">'.$key.':</span>'.$value.'</h3>';
												}
												else
													{
														
														$this->display_multi_array($value);
													}
										}
								}
							
						}
						else
							{
								echo '<h3>'.$array.'</h3>';
							}
			}

		private function set_sign($arr)
			{
				$sign='';
				if(!is_array($arr))
					{
						return null;
					}
				ksort($arr,SORT_STRING);
				foreach($arr as $key=>$value)
					{
						if($value!=null)
							{
								$sign.=$key.(is_array($value)?$this->get_sign($value):$value);
							}
							else
								{}
						
					}
				return $sign;
			}
	
		private function get_sign($sign,$token)
			{
				return strtoupper(md5(strtoupper(md5($sign)).$token));
			}
			
	
	}

	header('content-type:text/html;charset=utf-8');	
	ini_set('date.timezone','Asia/Shanghai');
	
	require_once $_SITE_PATH_ . "m/m_information.php";
	require_once $_SITE_PATH_ . "m/m_agreement.php";
	require_once $_SITE_PATH_ . "m/m_tasklist.php";
	require_once $_SITE_PATH_."m/m_taskerror.php";
	$m_agreement=new m_agreement();
	$m_information=new m_information();
	$m_tasklists=new m_tasklist();
	$m_taskerror=new m_taskerror();

	
	
	
	//$m_information->_ssql='select `id`,`code`,process_code,publish,`name`,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,create_user,create_date,originFile,processFile,finalFile FROM information where `flow`>=9 and ts_flag="1" and `code` is not null and agreement_id is not null and `code`!="" and agreement_id!="" limit 0,10';
	$m_information->_ssql='select information.id,`code`,process_code,publish,information.name,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,information.beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,shop_price,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,information.create_user,information.create_date,originFile,processFile,finalFile,is_vip,words,pages,agreement.start_time,agreement.end_time,authority as authority_id FROM information left join agreement on information.agreement_id=agreement.id where  `flow`>=9 and ts_flag!="1" and `code` is not null and agreement_id is not null and `code`!="" and process_code!="" and process_code is not null and agreement_id!="" and words!="" and agreement.start_time!="" and agreement.end_time!="" and process_code!="" and process_code!="0" and process_code is not null limit 180,15 ';
	
	$upfile_array=$m_information->getArray($m_information->query());
	
	
	
	$count=count($upfile_array);
	$id='';
	$count=count($upfile_array);
	for($i=0;$i<$count;$i++)
		{
			$id.=','.$upfile_array[$i]->id;
			unset($upfile_array[$i]->id);
		}
	$ts_id='';
	
	/* if(!empty($id))
		{
			$m_tasklists->_ssql='insert into tasklist(ts_type,product_id,excuflag) values("0","'.$id.'","0","")';
			$m_tasklists->query();
			$m_tasklists->_ssql='select last_insert_id() as lastid';
			$lastid=$m_tasklists->getArray($m_tasklists->query());
			$ts_id=$lastid[0]->lastid;
		} */
	
	
	/*
	$m_tasklists->_ssql='insert into tasklists( `id`,`code`,process_code,publish,`name`,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,create_user,create_date,originFile,processFile,finalFile,ts_flag) select `id`,`code`,process_code,publish,`name`,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,create_user,create_date,originFile,processFile,finalFile,ts_flag from information where `flow`>=9 and ts_flag!="1" limit 0,200';
	$m_tasklists->query();
									
									
	$m_tasklists->_ssql='select `id`,`code`,process_code,publish,`name`,isbn,bigclass_id,bigclass_name,smallclass_id,smallclass_name,`language`,`version`,price,num,`type`,r_date,editor,saler_sn,factory,author,print_page,publish_time,agreement_id,beizhu,is_upfile,catalog,p_cost,flow,is_volumes,books_nums,face_pic,plat_pic,copr_pic,code_pic,face_thumb,plat_thumb,copr_thumb,code_thumb,goods_file,create_user,create_date,originFile,processFile,finalFile,ts_flag FROM tasklists where `flow`>=9 and ts_flag!="1" limit 0,200 ';
	$upfile_array=$m_tasklists->getArray($m_tasklists->query());
									
	for($i=0;$i<count($upfile_array);$i++)
		{
			$m_information->_ssql='update information set ts_flag="1" where `id`='.$upfile_array[$i]->id;
			$m_information->query();
		} 
	*/
	
	//var_dump($upfile_array);
	$json=json_encode($upfile_array);
	/* echo $json;
	exit;
	$json=json_decode(trim(urldecode($upfile_array),chr(239).chr(187).chr(191)),true);
	$json=json_encode($json); */
	
	//echo $json;
	//echo $json;
	//var_dump($upfile_array);
	$auth='tjiePjfjvnyGUuyMFEXVPmsewokCauuZ';
//$data=array('data'=>$json,'apitype'=>'product','auth'=>urlencode($auth),'code'=>urlencode(md5(substr(md5($auth),5,15).md5($auth))));

$json_arr=array('data'=>$upfile_array,'count'=>$count);
//var_dump($json_arr);
$json_arr=json_encode($json_arr);
/* echo $json_arr;
echo '<br/><br/>'; */
/* echo $json_arr;
exit; */
//var_dump($json_arr);
$time=time();
$data=array('flag'=>'coprsys','method'=>'copr.add','type'=>'json','timestamp'=>$time,'charset'=>'utf-8','ver'=>1,'copr_data'=>$json_arr);
$url='http://120.86.185.197/index.php/openapi/rpc/service';//http://copr.hucai.com/jieshou.php    http://120.86.185.198/index.php/openapi/rpc/service/
//UpFile_ApiClient::GetInstance()->DoRequest($url,$auth,$data);
//var_dump();
/* 
$json_arr=array('data'=>$upfile_array,'count'=>$count);
$copr_data=json_encode($json_arr);
$data=array('flag'=>'','sign'=>'','method'=>'','type'=>'json','charset'=>'utf-8','ver'=>'1','timestamp'=>time(),'copr_data'=>$copr_data);
foreach($data as $key=>$value)
	{
		if($value===true)
			{
				$value=1;
			}
			elseif($value===false)
				{
					$value=0;
				}
				else
					{
						
					}
		if($value!=null)
			{
				$datastr.=$key.$value;
			}
			else
				{
					
				}
		
	}
	$data['sign']=strtoupper(md5(strtoupper(md5($datastr)).'miyao'));
	
	
	
	
	

 
 
 
 */

$api=new UpFile_ApiClient();
//var_dump($api);

$result=$api->DoRequest($url,$auth,$data);

//var_dump($result);
echo $result;
//var_dump($result);
/* 
if(get_magic_quotes_gpc())
	{
		$result=stripslashes($result);
	}
$result=json_decode(trim(urldecode($result),chr(239).chr(187).chr(191)),true);

if(strtolower($result['info'])=='success')
	{
		
		for($i=0;$i<$count;$i++)
			{
				$m_information->_ssql='update information set ts_flag="1" where `id`='.$upfile_array[$i]->id;
				$m_information->query();
				$m_tasklists->_ssql='update tasklist set excuflag="1" where `id`='.$ts_id;
				$m_tasklists->query();
			}
		echo $result['info'];
		exit;
	}
	elseif(strtolower($result['info'])=='failed')
		{
			$m_taskerror->_ssql='insert into taskerror(ts_type,product_id,excuflag) values("0","'.$id.'","0")';
			$m_taskerror->query();
			echo $result['info'];
			exit;
		}
		else
			{
				echo 'FAILED';
				exit;
			}
	
	 */

?>